﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BizMate.Domain;
using BizMate.WebUI.ViewModel;

namespace BizMate.WebUI.Controllers.Journal
{
    public class JournalController : Controller
    {
        AccountingEntities db = new AccountingEntities();
        //
        // GET: /Journal/

        public ActionResult Index()
        {
            ViewBag.Unit = new SelectList(db.Units.ToList(), "UnitId", "UnitName", string.Empty);
            ViewBag.Account = new SelectList(db.Accounts.ToList(), "AccountId", "Name", string.Empty);
            ViewBag.Type1 = new SelectList(new[]
                        {
                            new SelectListItem{Text="Debit", Value="1"},
                            new SelectListItem{Text="Credit", Value="2"}
                        }, "Value", "Text", "1");
            ViewBag.Type2 = new SelectList(new[]
                        {
                            new SelectListItem{Text="Debit", Value="1"},
                            new SelectListItem{Text="Credit", Value="2"}
                        }, "Value", "Text", "2");

            return View();
        }

        [HttpPost]
        public ActionResult Index(JournalViewModel journalViewModel)
        {
            if (ModelState.IsValid)
            {
                Vouchers voucher = new Vouchers
                {
                    VoucherNo = journalViewModel.VoucherNo,
                    VoucherDate = journalViewModel.VoucherDate,
                    UnitId = journalViewModel.UnitId,
                    Narration = journalViewModel.Narration
                };

                db.Vouchers.Add(voucher);
                db.SaveChanges();

                int _voucherId = voucher.VoucherId;

                VoucherDetails voucherDetails;

                foreach (var journal in journalViewModel.JounalDetailsList)
                {
                    voucherDetails = new VoucherDetails
                    {
                        VoucherId = _voucherId,
                        Type = journal.Type,
                        AccountId = journal.AccountId,
                        Amount = journal.Amount
                    };

                    db.VoucherDetails.Add(voucherDetails);
                    db.SaveChanges();
                }

                RedirectToAction("Index");
            }

            ViewBag.Unit = new SelectList(db.Units.ToList(), "UnitId", "UnitName", string.Empty);
            ViewBag.Account = new SelectList(db.Accounts.ToList(), "AccountId", "Name", string.Empty);
            ViewBag.Type1 = new SelectList(new[]
                        {
                            new SelectListItem{Text="Debit", Value="1"},
                            new SelectListItem{Text="Credit", Value="2"}
                        }, "Value", "Text", "1");
            ViewBag.Type2 = new SelectList(new[]
                        {
                            new SelectListItem{Text="Debit", Value="1"},
                            new SelectListItem{Text="Credit", Value="2"}
                        }, "Value", "Text", "2");

            return View(journalViewModel);
        }

    }
}
